<?php
namespace Api\Controller;

use Think\Controller;
use Base\FrontController;

/**
 * @name 前端商品相关功能提交和查询数据 ，访问本接口下任何方法均要求session中已保留注册用户信息
 * swagger-ui api文档生成cmd： php D:\work\php\swagger-php\vendor\zircote\swagger-php\bin\swagger D:\work\php\wx-third-platform\code\Application\Api\Controller\swagger D:\work\php\wx-third-platform\code\Application\Api\Controller\ApiBHCommodityController.class.php  -o D:\work\php\wx-third-platform\code\swui\商品-commodity.json
 * @author perry
 *
 */
class ApiBHCommodityController extends FrontController
{

    // private $weixin_user;

    function _initialize()
    {
        parent::_initialize();
        $this->closetM=D('Home/BHCloset');
        $this->closetFavoriteM=D('Home/BHClosetFavorite');
        $this->uid=$this->weixin_user['id'];
    }

     /**
     * @SWG\Get(
     *     path="/ApiBHCommodity/getCommodityListByPage",
     *     tags={"getCommodityListByPage"},
     *     description="分页获取商品列表数据",
     *     operationId="getCommodityListByPage",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="t",
     *         in="query",
     *         description="分类id,不传代表所有(来自接口/Api/BHType/getAll)",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="p",
     *         in="query",
     *         description="指定页码(缺省为1)",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="ps",
     *         in="query",
     *         description="每页容量(缺省为10)",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="name",
     *         in="query",
     *         description="模糊搜索词",
     *         required=false,
     *         type="string"
     *         
     *     ),
     *      @SWG\Parameter(
     *         name="id",
     *         in="query",
     *         description="指定唯一id查找一条数据",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *         
     *     ),
     *      @SWG\Parameter(
     *         name="order",
     *         in="query",
     *         description="指定排序方式，不传值按更新时间倒序，传favorite将把当前用户收藏过的商品排在最前面",
     *         required=false,
     *         type="string",
     *         
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="指定条件下的商品每页数据",
     *         @SWG\Schema(
     *             ref="#/definitions/commoditys"
     *         ),
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function getCommodityListByPage(){
        $type=I('t',1);
        $page=I('p',1);
        $pagesize=I('ps',10);
        $name=I('name',false);
        $id=I('id',-1);
        $order=I('order','default');
        $status=1;
        //getByPage($uid=0,$type=1,$status=1,$name=false,$page=1,$pageSize=10,$field=true){
        if($order=='favorite'){
            //要求把当前用户收藏的显示在最前面返回数据
            $re=$this->closetM->getByPageWithFavoriteTop('commodity',$this->uid,C('super_style_uid'),$type,$status,$name,$page,$pagesize,'id,uid,type,img,name,memo,sub_imgs,no,price,dh_date,show_order,update_time,color,size',$id);
        }
        else {
            //按 update_time 倒序获取数据
            $re=$this->closetM->getByPage('commodity',C('super_style_uid'),$type,$status,$name,$page,$pagesize,'id,uid,type,img,name,memo,sub_imgs,no,price,dh_date,show_order,update_time,color,size',$id);
        }
        // dump($re);
        $this->customReturn(1,$re);
        
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCommodity/setFavorite",
     *     tags={"commodity-setFavorite"},
     *     description="收藏百家好商品",
     *     operationId="commodity-setFavorite",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="cc_id",
     *         in="query",
     *         description="商品id",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="收藏结果（>0成功，其它失败）"
     *         
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function setFavorite(){
        try {
            $cc_id=I('cc_id',false);
            $re=$this->closetFavoriteM->addOne($this->uid,$cc_id);
            // dump($re);
            $this->customReturn(1,$re);
        } catch (\Exception $exc) {
            $succ = $exc->getCode() == 1 ? -1 : $exc->getCode();
            $datas = array('errcode' => $succ, 'errmsg' => $exc->getMessage());
            $this->customReturn($succ, $datas);
        }
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCommodity/unsetFavorite",
     *     tags={"commodity-unsetFavorite"},
     *     description="删除已收藏的数据",
     *     operationId="commodity-unsetFavorite",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="cc_id",
     *         in="query",
     *         description="待删除收藏的商品主键id",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     
     *     @SWG\Response(
     *         response=200,
     *         description="删除成功与否结果",
     *         
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function unsetFavorite(){
        $id=I('cc_id',-1);
        try {
            $re=$this->closetFavoriteM->deleteOne($this->uid,$id);
            $this->customReturn(1,$re);
            
        } catch (\Exception $exc) {
            $succ = $exc->getCode() == 1 ? -1 : $exc->getCode();
            $datas = array('errcode' => $succ, 'errmsg' => $exc->getMessage());
            $this->customReturn($succ, $datas);
        }
        
        
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCommodity/isFavorite",
     *     tags={"commodity-isFavorite"},
     *     description="判断指定商品当前用户是否已收藏",
     *     operationId="commodity-isFavorite",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="cc_id",
     *         in="query",
     *         description="待判断商品",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     
     *     @SWG\Response(
     *         response=200,
     *         description="是否已收藏，-1否,其它值表示已收藏的收藏时间",
     *         
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function isFavorite(){
        $id=I('cc_id',-1);
        try {
            $re=$this->closetFavoriteM->getOne($this->uid,$id);
            $re=$re?$re['create_time']:-1;
            $this->customReturn(1,$re);
            
        } catch (\Exception $exc) {
            $succ = $exc->getCode() == 1 ? -1 : $exc->getCode();
            $datas = array('errcode' => $succ, 'errmsg' => $exc->getMessage());
            $this->customReturn($succ, $datas);
        }
        
        
    }



    

}
